﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Timers;

using GG.Mail;
using GG.InputOut;
    
namespace GG.CarInsuranceRenewal
{
    public partial class SendCarInsuranceRenewalMail : ServiceBase
    {
        EventLog _eventLog = new EventLog();
        int _remiderDay = 21;
        int _remiderHour = 6;
        int _remiderMin = 20;
        int _remiderEmailInterval = 25;
        int _timerNormalInterval = 10;

        public SendCarInsuranceRenewalMail()
        {
            InitializeComponent();
            //if (!System.Diagnostics.EventLog.SourceExists("GG_CarInsuranceRenewalMailService"))
            //    System.Diagnostics.EventLog.CreateEventSource("GG_CarInsuranceRenewalMailService",
            //                                                          "GG Car Insurance Renewal Mail Service");
            _eventLog.Source = "GG_CarInsuranceRenewalMailService";
        }

        protected override void OnStart(string[] args)
        {
            _remiderDay = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["ReminderDay"].ToString());
            _remiderHour = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["ReminderHour"].ToString());
            _remiderMin = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["ReminderMin"].ToString());
            _remiderEmailInterval = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["ReminderEmailInterval"].ToString());
            _timerNormalInterval = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["TimerNormalInterval"].ToString());

            timer1.Interval = _timerNormalInterval * 1000;
            timer1.Enabled = true;
            _eventLog.WriteEntry("Current DateTime:" + GG.Global.General.GetUTCDateTime().ToString() + ", Reminder Setting : Day:" + _remiderDay.ToString() + ", H:" + _remiderHour.ToString() + ", M:" + _remiderMin.ToString(), EventLogEntryType.Information);
            _eventLog.WriteEntry("Timer Interval:" + timer1.Interval.ToString(), EventLogEntryType.Information);
        }

        private void WriteEventLog(string Description)
        {

        }

        protected override void OnStop()
        {
            timer1.Stop();
            //_eventLog.WriteEntry("CX - Policy Listing OnStop", EventLogEntryType.Information);
        }

        private string CreatePolicyFile(string strFileContent, string strFileName)
        {
            String[] _fileContent = new String[1];
            _fileContent[0] = strFileContent;

            return WritePolicyFile(strFileName, _fileContent);
        }

        private string WritePolicyFile(string strPrefixFileName, string[] strFileContent)
        {
            string strResult = "";
            try
            {
                MyIO _newFile = new MyIO();
                string _filePath = System.Configuration.ConfigurationSettings.AppSettings["sharedloc"].ToString();
                //"D:\\SharedLoc\\PolicyFiles\\";
                if (_newFile.CheckDirectory(_filePath))
                {
                    string _curMonth = GG.Global.General.GetUTCDateTime().Year.ToString("0000") + GG.Global.General.GetUTCDateTime().Month.ToString("00");

                    if (!_newFile.CheckDirectory(_filePath + _curMonth))
                    {
                        if (_newFile.CreateDirectory(_filePath, _curMonth))
                        {
                            _filePath += _curMonth + "\\";
                        }
                    }
                    else
                    {
                        _filePath += _curMonth + "\\";
                    }

                    string _fileName = strPrefixFileName + _curMonth + GG.Global.General.GetUTCDateTime().Day.ToString("00") + GG.Global.General.GetUTCDateTime().Second.ToString("00") + GG.Global.General.GetUTCDateTime().Millisecond.ToString("00") + ".xls";

                    _newFile.WriteNewFile(_filePath, _fileName, strFileContent);
                    _newFile = null;
                    strResult = _filePath + _fileName;
                }
            }
            catch (Exception Ex)
            {
                Ex = null;
            }

            return strResult;
        }

        private string GetThaiMonth(string Month)
        {
            string _monthName = "";
            if (Month == "01")
            {
                _monthName = "มกราคม";
            }
            else if (Month == "02")
            {
                _monthName = "กุมภาพันธ์";
            }
            else if (Month == "03")
            {
                _monthName = "มีนาคม";
            }
            else if (Month == "04")
            {
                _monthName = "เมษายน";
            }
            else if (Month == "05")
            {
                _monthName = "พฤษภาคม";
            }
            else if (Month == "06")
            {
                _monthName = "มิถุนายน";
            }
            else if (Month == "07")
            {
                _monthName = "กรกฎาคม";
            }
            else if (Month == "08")
            {
                _monthName = "สิงหาคม";
            }
            else if (Month == "09")
            {
                _monthName = "กันยายน";
            }
            else if (Month == "10")
            {
                _monthName = "ตุลาคม";
            }
            else if (Month == "11")
            {
                _monthName = "พฤศจิกายน";
            }
            else if (Month == "12")
            {
                _monthName = "ธันวาคม";
            }
            return _monthName;
        }

        private string GetThaiYear(string Year)
        {
            string _thaiYear  = (Convert.ToInt32(Year) + 543).ToString();
            return _thaiYear;
        }

        private void Timer1_Elapsed(object sender,
         System.Timers.ElapsedEventArgs e)
        {
        //    //start test
        //    StartSendingEmail();
        //}

        //public void StartService()
        //{
        //    StartSendingEmail();
        //    //timer1.Enabled = false;
        //}
        //private void StartSendingEmail()
        //{
        //    //end test

            DateTime _dtReminderDate = GG.Global.General.GetUTCDateTime();
            //_eventLog.WriteEntry("Current DateTime:" + _dtReminderDate.ToString() + "Current Interval:" + timer1.Interval.ToString() + ", Reminder Setting : Day:" + _remiderDay.ToString() + ", H:" + _remiderHour.ToString() + ", M:" + _remiderMin.ToString(), EventLogEntryType.Information);

            //if (_dtReminderDate.Day == _remiderDay && _dtReminderDate.Hour == _remiderHour && _dtReminderDate.Minute == _remiderMin)
            //{
                timer1.Enabled = false;
                timer1.Interval = 24 * 60 * 60;

                string _conString = "";
                SqlCommand _sqlCmd = null;
                SqlConnection _sqlConn = new SqlConnection();
                DateTime _dtNow = GG.Global.General.GetUTCDateTime();
                _dtNow = _dtNow.AddMonths(2);

                string _emailTitle = "REMINDER - Car Insurance Renewal Expiring in " + _dtNow.ToString("MMM yyyy");
                string[] fileList = new string[0];
                string _toemail = "";
                string _ccemail = "";
                string _bccemail = "";
                string _emailBody = "";
                string _userEmailTitle = "";
                string _CustEmailTitle = "";
                string _Language = "TH";


                string _step = "Step 1";
                try
                {
                    _conString = ConfigurationManager.ConnectionStrings["GG_DBConnectionString"].ToString();


                    _sqlConn.ConnectionString = _conString;


                    DataTable _dtResult = new DataTable("dtResult");
                    _sqlConn.Open();
                    _sqlCmd = new SqlCommand();
                    _sqlCmd.Connection = _sqlConn;
                    _sqlCmd.CommandType = CommandType.StoredProcedure;
                    _sqlCmd.CommandText = "[sprocSIRSelectItemListByRenewalMonth]";
                    SqlParameter _p = new SqlParameter("@CarRenewalMonth", _dtNow.Month);
                    _sqlCmd.Parameters.Add(_p);

                    SqlDataAdapter _sqlDa = new SqlDataAdapter(_sqlCmd);
                    _sqlDa.Fill(_dtResult);
                    _sqlConn.Close();
                    //if (_Language == "EN")
                    //{
                    //    //_CustEmailTitle = "Car Insurance Renewal Reminder";
                    //    _CustEmailTitle = "REMINDER - Car Insurance Renewal Expiring in " + _dtNow.ToString("MMM yyyy");
                    //}
                    //else if (_Language == "TH")
                    //{
                    //    //_CustEmailTitle = "แจ้งเตือนการต่ออายุประกันภัยรถยนต์";
                    //    _CustEmailTitle = "แจ้งเตือน - การต่ออายุประกันภัยรถยนต์ สิ้นสุดการคุ้มครอง เดือน " + GetThaiMonth(_dtNow.Month.ToString("00")) + " " + GetThaiYear(_dtNow.Year.ToString("0000"));
                    //}
                    _userEmailTitle = "REMINDER - Car Insurance Renewal Expiring in " + _dtNow.ToString("MMM yyyy");
                    //_emailTitle = "GG - Car Insurance Renewal Mail Service Started at " + GG.Global.General.GetUTCDateTime().ToString();
                    //_emailBody = "Title:" + _userEmailTitle + "<br/><br/>This is just test message from Car Insurance Renewal Reminder Mail Service for " + _dtNow.ToString("MMM yyyy");
                    //_eventLog.WriteEntry("Car Insurance Renewal Reminder Mail Service Started at [" + GG.Global.General.GetUTCDateTime().ToString() + "].", EventLogEntryType.Information);
                    _step = "Step 2";

                    //if (MyMailManager.SendEmailByAttachedment(_emailTitle, _emailBody, _toemail, _ccemail, _bccemail, fileList))
                    //{
                        //System.Text.StringBuilder _strEmailList = new StringBuilder();
                        //System.Text.StringBuilder _strFaillEmailList = new StringBuilder();

                        try
                        {
                            //9/12/2556 save to tblWindowServiceLog
                            _sqlCmd = new SqlCommand();
                            _sqlCmd.Connection = _sqlConn;
                            _sqlCmd.CommandType = CommandType.StoredProcedure;
                            _sqlCmd.CommandText = "[sprocWindowServiceSaveLog]";
                            _sqlCmd.Parameters.AddWithValue("@ServiceName", "GG_CarInsuranceRenewalMailService");
                            _sqlCmd.Parameters.AddWithValue("@Subject", "GG Car Insurance Renewal Mail Service was started at " + MyMailManager.GetUTCDateTime().ToString("dd/MM/yyyy hh:mm:ss"));
                            _sqlCmd.Parameters.AddWithValue("@EmailCount", _dtResult.Rows.Count);
                            _sqlCmd.Parameters.AddWithValue("@ServiceStatus", 1);
                            _sqlCmd.Parameters.AddWithValue("@Status", 1);
                            _sqlConn.Open();
                            _sqlCmd.ExecuteNonQuery();
                            _sqlConn.Close();
                        }
                        catch (Exception ex)
                        {
                            string xx = ex.Message.ToString();
                        }

                        _step = "Step 2.1";
                        if (_dtResult != null && _dtResult.Rows.Count > 0)
                        {
                            _step = "Step 2.2";
                            int _mailSendCount = 0;
                            char _bccWithCusEnFlag = '0', _bccWithCusThFlag = '0'; //never bcc = '0'. after bcc 1 time change to '1' 
                            _step = "Step 2.3";
                            _toemail = System.Configuration.ConfigurationSettings.AppSettings["toemail"].ToString();
                            _ccemail = System.Configuration.ConfigurationSettings.AppSettings["ccemail"].ToString();
                            for (int i = 0; i < _dtResult.Rows.Count; i++)
                            {
                                _step = "Step 3";

                                if (System.Configuration.ConfigurationSettings.AppSettings["IsCustomerEmailCheck"].ToString() == "1")
                                {
                                    _bccemail = System.Configuration.ConfigurationSettings.AppSettings["BccWithCustomerEmail"].ToString();
                                }
                                _Language = _dtResult.Rows[i]["Language"].ToString();
                                string _touseremail = _dtResult.Rows[i]["Email"].ToString();//"niran.s@comparexpress.com";//
                                System.Text.StringBuilder _strBody = new StringBuilder();
                                string domainUrl = "http://www.gluaygluay.com";

                                if (_Language == "EN")
                                {
                                    _CustEmailTitle = "REMINDER - Car Insurance Renewal Expiring in " + _dtNow.ToString("MMM yyyy", new System.Globalization.CultureInfo("en-US"));                                   
                                    if (_bccWithCusEnFlag == '1')
                                        _bccemail = "";
                                    _bccWithCusEnFlag = '1';
                                    _strBody.Append("<style type=\"text/css\">a {text-decoration:underline;color:#2c95cc;}</style>");
                                    _strBody.Append("<center>");


                                    _strBody.Append(@"<table width='800' height='100' border='0' cellspacing='0' cellpadding='0'>
                                                          <tr>");
                                    _strBody.Append("<td><span class='email-banner'><img src='" + domainUrl + "/images/email-header.jpg' width='800' height='100' alt=''/></span></td>");
                                    _strBody.Append(@"</tr>
                                                        </table>");

                                    _strBody.Append("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"600\" style=\"color:#4c4c4c; font-family:Arial, Helvetica, sans-serif; font-size:14px;\">");
                                    _strBody.Append("<tr>");
                                    _strBody.Append("<td>");
                                    _strBody.Append("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\">");
                                    _strBody.Append("<tr>");
                                    _strBody.Append("<td width=\"45\"></td>");
                                    _strBody.Append("<td>");
                                    _strBody.Append("<br/><br/>");
                                    _strBody.Append("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\">");
                                    _strBody.Append("<tr>");
                                    _strBody.Append("<td width=\"450\" valign=\"top\" style=\"font-family: Arial, Helvetica, sans-serif;\">");
                                    _strBody.Append("<span style=\"font-family: Arial, Helvetica, sans-serif;\">");
                                    _strBody.Append("Dear <strong>" + _dtResult.Rows[i]["FullName"].ToString() + "</strong>,");
                                    _strBody.Append("<br/><br/>");
                                    _strBody.Append("Thank you for setting your car insurance renewal reminder on GluayGluay.com.");
                                    _strBody.Append("<br/><br/>");
                                    _strBody.Append("We would like to inform you that your car insurance will be expiring soon. You can now compare instant and actual quotes and purchase your policy online in 2 minutes by clicking on the following link <a href=\"http://www.gluaygluay.com/en\"><strong>www.gluaygluay.com</strong></a>.");
                                    _strBody.Append("<br/><br/>");
                                    _strBody.Append("Hope you will enjoy using our services.");
                                    _strBody.Append("<br/><br/>");
                                    _strBody.Append("</td>");
                                    _strBody.Append("<td width=\"90\"></td>");
                                    _strBody.Append("<td width=\"130\" valign=\"top\" style=\"font-family: Arial, Helvetica, sans-serif; font-size:12px; color:#8c8c8c;\">");
                                    _strBody.Append("<span style=\"font-family: Arial, Helvetica, sans-serif;\">");
                                    _strBody.Append("<br/><br/>");
                                    _strBody.Append("<strong></strong><br/>");
                                    _strBody.Append("<a href=\"http://www.gluaygluay.com/en/\"><strong>Quote Now</strong></a><br/><br/>");
                                    _strBody.Append("Created Date: <br/>");
                                    _step = "Step 3.1";
                                    _strBody.Append("<em>" + Convert.ToDateTime(_dtResult.Rows[i]["CreatedDate"].ToString()).ToString("dd/MM/yyyy") + "</em>");
                                    _step = "Step 3.2";
                                    _strBody.Append("<span>");
                                    _strBody.Append("</td>");
                                    _strBody.Append("</tr>");
                                    _strBody.Append("</table>");
                                    _strBody.Append("</td>");
                                    _strBody.Append("<td width=\"45\"></td>");
                                    _strBody.Append("</tr>");
                                    _strBody.Append("</table>");
                                    _strBody.Append("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\">");
                                    _strBody.Append("<tr>");

                                    _strBody.Append(@"
                                                        <td colspan='2' align='left' style='font-style: normal; font-size: 14px;'>With Best Regards,</td>
                                                      </tr>
              
                                                    </table>
                                                    <hr>
                                                    <p><b>GluayGluay Customer Service Team</b><br>
                                                      CompareXpress Insurance Broker (Thailand) Ltd.<br>
                                                     OIC  License number ว00016/2553  <br>
                                                      42nd Floor, United Center Building,<br>
                                                      323 Silom Road, Bangrak,<br>
                                                      Bangkok 10500</p>
                                                    <p>Tel : 02-268-8222 <br>
                                                      Fax : 02-266-6339 <br>
                                                  <a href='mailto:support@gluaygluay.com'>support@gluaygluay.com</a></p></td>
                                                </tr>
                                              </table>
                                              <hr>
      
                                              <table width='300' border='0' align='center' cellpadding='0' cellspacing='0'>
                                                <tr>");
                                    _strBody.Append("<td width='44' style='text-align: center'><a href='https://www.facebook.com/GGmotor'><img src='" + domainUrl + "/images/icons/fb-big.png' width='44' height='42' alt=''/></a></td>");
                                    _strBody.Append("<td width='44' style='text-align: center'><a href='https://plus.google.com/+Gluaygluay/posts'><img src='" + domainUrl + "/images/icons/g-plus-big.png' width='44' height='42' alt=''/></a></td>");
                                    _strBody.Append("<td width='44' style='text-align: center'><a href='https://www.youtube.com/channel/UCZ4gMtlXymBHxvcXO2cR1vA'><img src='" + domainUrl + "/images/icons/youtube-big.png' width='44' height='42' alt=''/></a></td>");
                                    _strBody.Append("<td width='44' style='text-align: center'><a href='http://iconosquare.com/ggmotor'><img src='" + domainUrl + "/images/icons/instagram-big.png' width='44' height='42' alt=''/></a></td>");
                                    _strBody.Append("<td width='43' style='text-align: center'><a href='https://www.linkedin.com/company/-com-gluaygluay-com-'><img src='" + domainUrl + "/images/icons/linkedin-big.png' width='44' height='42' alt=''/></a></td>");
                                    _strBody.Append(@" </tr>
                                              </table>");
                                    _strBody.Append("<table width='800' height='100' border='0' cellspacing='0' cellpadding='0' style=\"background-image: url('" + domainUrl + "/images/email-footer.jpg');\">");
                                    _strBody.Append(@"<tr>
                                                  <td><p style='text-align: center'><br><br>
                                                    © ");
                                    _strBody.Append(DateTime.Now.ToString("yyyy", new System.Globalization.CultureInfo("en-US")));
                                    _strBody.Append(@" gluaygluay.com. All rights reserved.<br>
                                                      This is a computer-generated email. Please do not reply to this email. </p></td>
                                                </tr>
                                              </table></td>");

                                    _strBody.Append("<td width=\"45\"></td>");
                                    _strBody.Append("</tr>");
                                    _strBody.Append("</table>");
                                    _strBody.Append("<br/><br/>");
                                    _strBody.Append("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" style=\"background:#efefef\" width=\"510\" align=\"center\">");
                                    _strBody.Append("<tr>");
                                    _strBody.Append("<td height=\"25\" align=\"center\" style=\"font-family: Arial, Helvetica, sans-serif; color:#8b8b8b; font-size:10px;\">");
                                    _strBody.Append("<span style=\"font-family: Arial, Helvetica, sans-serif;\">");
                                    _strBody.Append("If you want to stop receiving this reminder, please  <a href=\"http://www.gluaygluay.com/en/contactus/\" style=\"color:#8b8b8b;\">Unsubscribe</a>");
                                    _strBody.Append("</span>");
                                    _strBody.Append("</td>");
                                    _strBody.Append("</tr>");
                                    _strBody.Append("</table>");
                                    _strBody.Append("<br/>");

                                    _strBody.Append("</td>");
                                    _strBody.Append("</tr>");
                                    _strBody.Append("</table>");
                                    _strBody.Append("</center>");
                                }
                                else if (_Language == "TH")
                                {
                                    _CustEmailTitle = "แจ้งเตือน - การต่ออายุประกันภัยรถยนต์ สิ้นสุดการคุ้มครอง เดือน " + GetThaiMonth(_dtNow.Month.ToString("00")) + " " + GetThaiYear(_dtNow.Year.ToString("0000"));

                                    if (_bccWithCusThFlag == '1')
                                        _bccemail = "";
                                    _bccWithCusThFlag = '1';

                                    _strBody.Append("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
                                    _strBody.Append("<head id=\"Head1\" runat=\"server\">");
                                    _strBody.Append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-874\" />");
                                    _strBody.Append("<style type=\"text/css\">a {text-decoration:underline;color:#2c95cc;}</style>");
                                    _strBody.Append("</head><body>");
                                    _strBody.Append("<center>");
                                    _strBody.Append(@"<table width='800' height='100' border='0' cellspacing='0' cellpadding='0'>
                                                          <tr>");
                                    _strBody.Append("<td><span class='email-banner'><img src='" + domainUrl + "/images/email-header.jpg' width='800' height='100' alt=''/></span></td>");
                                    _strBody.Append(@"</tr>
                                                        </table>");

                                    _strBody.Append("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"600\" style=\"color:#4c4c4c; font-family:Arial, Helvetica, sans-serif; font-size:14px;\">");
                                    _strBody.Append("<tr>");
                                    _strBody.Append("<td>");
                                    _strBody.Append("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\">");
                                    _strBody.Append("<tr>");
                                    _strBody.Append("<td width=\"45\"></td>");
                                    _strBody.Append("<td>");
                                    _strBody.Append("<br/><br/>");
                                    _strBody.Append("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\">");
                                    _strBody.Append("<tr>");
                                    _strBody.Append("<td width=\"450\" valign=\"top\" style=\"font-family: Arial, Helvetica, sans-serif;\">");
                                    _strBody.Append("<span style=\"font-family: Arial, Helvetica, sans-serif;\">");
                                    _strBody.Append("เรียนคุณ <strong>" + _dtResult.Rows[i]["FullName"].ToString() + "</strong>,");
                                    _strBody.Append("<br/><br/>");
                                    _strBody.Append("ขอบคุณที่ใช้บริการแจ้งเตือนการต่ออายุประกันรถยนต์ ที่เว็บไซต์ www.gluaygluay.com.");
                                    _strBody.Append("<br/><br/>");
                                    _strBody.Append("ขณะนี้ประกันรถยนต์ของท่านกำลังจะหมดอายุลง   ด้วยเวลาเพียงไม่กี่นาที ท่านสามารถเปรียบเทียบราคาและความคุ้มครองพร้อมทั้งซื้อกรมธรรม์ประกันรถยนต์ผ่านระบบ online ได้ทันทีที่  <a href=\"http://www.gluaygluay.com\"><strong>www.gluaygluay.com</strong></a>.");
                                    _strBody.Append("<br/><br/>");
                                    _strBody.Append("</td>");
                                    _strBody.Append("<td width=\"90\"></td>");
                                    _strBody.Append("<td width=\"130\" valign=\"top\" style=\"font-family: Arial, Helvetica, sans-serif; font-size:12px; color:#8c8c8c;\">");
                                    _strBody.Append("<span style=\"font-family: Arial, Helvetica, sans-serif;\">");
                                    _strBody.Append("<br/><br/>");
                                    _strBody.Append("<strong></strong><br/>");
                                    _strBody.Append("<a href=\"http://www.gluaygluay.com/\"><strong>Quote Now</strong></a><br/><br/>");
                                    _strBody.Append("Created Date: <br/>");
                                    _step = "Step 3.1";
                                    _strBody.Append("<em>" + Convert.ToDateTime(_dtResult.Rows[i]["CreatedDate"].ToString()).ToString("dd/MM/yyyy") + "</em>");
                                    _step = "Step 3.2";
                                    _strBody.Append("<span>");
                                    _strBody.Append("</td>");
                                    _strBody.Append("</tr>");
                                    _strBody.Append("</table>");
                                    _strBody.Append("</td>");
                                    _strBody.Append("<td width=\"45\"></td>");
                                    _strBody.Append("</tr>");
                                    _strBody.Append("</table>");
                                    _strBody.Append("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\">");
                                    _strBody.Append("<tr>");

                                    _strBody.Append(@"<td colspan='2' align='left' style='font-style: normal; font-size: 14px;'>ด้วยความนับถือ,</td>
                                                      </tr>
                                                    </table>
                                                    <hr>");
                                    _strBody.Append("<span style=\"color:red; font-size:25px;\">โปรดระวังมิจฉาชีพแอบอ้างเป็นโบรกเกอร์กล้วยๆ<br />หรือแอบอ้างเป็นศูนย์รถหรือได้เบอร์มาจากบริษัทประกันภัย</span><br />");
                                    _strBody.Append("เจ้าหน้าที่ของเราจะติดต่อท่านด้วยหมายเลข 02 646 9101 - 16 เท่านั้น<br />");
                                    _strBody.Append("สามารถตรวจสอบข้อมูลเพิ่มเติมได้ที่");
                                    _strBody.Append("<a  style=\"color:rgb(59,89,152);text-decoration:none\" href=\"http://www.gluaygluay.com\">www.GluayGluay.com</a>");
                                    _strBody.Append("<br />หรือโทร <a href=\"tel:022688222\"><strong>0-2268-8222</strong></a><br />");
                                    _strBody.Append(@"<p><b>Support GluayGluay.com</b><br>
                                                      บริษัท คอมแพร์เอ็กซ์เพรส นายหน้าประกันภัย (ประเทศไทย) จำกัด<br>
                                                    เลขทะเบียนนายหน้าประกันวินาศภัยนิติบุคคล เลขที่ ว00016/2553  <br>
                                                      ชั้น 42 อาคารยูไนเต็ด เซ็นเตอร์,<br>
                                                      เลขที่ 323 ถนนสีลม แขวงสีลม เขตบางรัก,<br>
                                                      กรุงเทพฯ 10500</p>
                                                    <p>Tel : 02-268-8222 <br>
                                                      Fax : 02-266-6339 <br>
                                                  <a href='mailto:support@gluaygluay.com'>support@gluaygluay.com</a></p></td>
                                                </tr>
                                              </table>
                                              <hr>
      
                                              <table width='300' border='0' align='center' cellpadding='0' cellspacing='0'>
                                                <tr>");
                                    _strBody.Append("<td width='44' style='text-align: center'><a href='https://www.facebook.com/GGmotor'><img src='" + domainUrl + "/images/icons/fb-big.png' width='44' height='42' alt=''/></a></td>");
                                    _strBody.Append("<td width='44' style='text-align: center'><a href='https://plus.google.com/+Gluaygluay/posts'><img src='" + domainUrl + "/images/icons/g-plus-big.png' width='44' height='42' alt=''/></a></td>");
                                    _strBody.Append("<td width='44' style='text-align: center'><a href='https://www.youtube.com/channel/UCZ4gMtlXymBHxvcXO2cR1vA'><img src='" + domainUrl + "/images/icons/youtube-big.png' width='44' height='42' alt=''/></a></td>");
                                    _strBody.Append("<td width='44' style='text-align: center'><a href='http://iconosquare.com/ggmotor'><img src='" + domainUrl + "/images/icons/instagram-big.png' width='44' height='42' alt=''/></a></td>");
                                    _strBody.Append("<td width='43' style='text-align: center'><a href='https://www.linkedin.com/company/-com-gluaygluay-com-'><img src='" + domainUrl + "/images/icons/linkedin-big.png' width='44' height='42' alt=''/></a></td>");
                                    _strBody.Append(@" </tr>
                                              </table>");
                                    _strBody.Append("<table width='800' height='100' border='0' cellspacing='0' cellpadding='0' style=\"background-image: url('" + domainUrl + "/images/email-footer.jpg');\">");
                                    _strBody.Append(@"<tr>
                                                  <td><p style='text-align: center'><br><br>
                                                    © ");
                                    _strBody.Append(DateTime.Now.ToString("yyyy", new System.Globalization.CultureInfo("en-US")));
                                    _strBody.Append(@" gluaygluay.com. All rights reserved.<br>
                                                      อีเมลนี้ถูกส่งจากระบบไม่ต้องการการตอบกลับ </p></td>
                                                </tr>
                                              </table></td>");

                                    _strBody.Append("<td width=\"45\"></td>");
                                    _strBody.Append("</tr>");
                                    _strBody.Append("</table>");
                                    _strBody.Append("<br/><br/>");
                                    _strBody.Append("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" style=\"background:#efefef\" width=\"510\" align=\"center\">");
                                    _strBody.Append("<tr>");
                                    _strBody.Append("<td height=\"25\" align=\"center\" style=\"font-family: Arial, Helvetica, sans-serif; color:#8b8b8b; font-size:10px;\">");
                                    _strBody.Append("<span style=\"font-family: Arial, Helvetica, sans-serif;\">");
                                    _strBody.Append("If you want to stop receiving this reminder, please  <a href=\"http://www.gluaygluay.com/contactus/\" style=\"color:#8b8b8b;\">ยกเลิกสมาชิก</a>");
                                    _strBody.Append("</span>");
                                    _strBody.Append("</td>");
                                    _strBody.Append("</tr>");
                                    _strBody.Append("</table>");
                                    _strBody.Append("<br/>");

                                    _strBody.Append("</td>");
                                    _strBody.Append("</tr>");
                                    _strBody.Append("</table>");

                                    _strBody.Append("</center>");
                                    _strBody.Append("</body></html>");
                                }
                                if (MyMailManager.SendGGMail(_CustEmailTitle, _strBody.ToString(),_touseremail , _ccemail, _bccemail))
                                {
                                    _mailSendCount++;
                                    _bccemail = "";
                                }
                                else
                                {
                                    // _eventLog.WriteEntry("FAILED: " + _touseremail + " - " + _userEmailTitle + " -  at [" + GG.Global.General.GetUTCDateTime().ToString() + "].", EventLogEntryType.Error);
                                }
                                _strBody = null;
                                System.Threading.Thread.Sleep(_remiderEmailInterval * 1000);
                            }


                            _step = "Step 4";
                            //9/12/2556 save to tblWindowServiceLog
                            try
                            {
                                _sqlCmd = new SqlCommand();
                                _sqlCmd.Connection = _sqlConn;
                                _sqlCmd.CommandType = CommandType.StoredProcedure;
                                _sqlCmd.CommandText = "[sprocWindowServiceSaveLog]";
                                _sqlCmd.Parameters.AddWithValue("@ServiceName", "GG_CarInsuranceRenewalMailService");
                                _sqlCmd.Parameters.AddWithValue("@Subject", "GG - Car Insurance Renewal Reminder Mail Service was finished at " + MyMailManager.GetUTCDateTime().ToString("dd/MM/yyyy hh:mm:ss"));
                                _sqlCmd.Parameters.AddWithValue("@EmailCount", _mailSendCount);
                                _sqlCmd.Parameters.AddWithValue("@ServiceStatus", 1);
                                _sqlCmd.Parameters.AddWithValue("@Status", 1);
                                _sqlConn.Open();
                                _sqlCmd.ExecuteNonQuery();
                                _sqlConn.Close();
                            }
                            catch { }
                            //_emailTitle = "GG - Car Insurance Renewal Reminder Mail Service Finished at " + GG.Global.General.GetUTCDateTime().ToString();
                            //_emailBody = "<strong>Title:</strong>" + _userEmailTitle;
                            //_emailBody += "<br/><br/>" + _mailSendCount.ToString() + " messages were sent successfully. <br/><br/>This is just test message from Car Insurance Renewal Mail Service for " + _dtNow.ToString("MMM yyyy");

                            //if (MyMailManager.SendEmailByAttachedment(_emailTitle, _emailBody, _toemail, _ccemail, _bccemail, fileList))
                            //{
                            //    //_eventLog.WriteEntry(_mailSendCount.ToString() + " messages were sent successfully. [" + GG.Global.General.GetUTCDateTime().ToString() + "].", EventLogEntryType.SuccessAudit);
                            //}
                            //else
                            //{
                            //    //_eventLog.WriteEntry("Car Insurance Renewal Reminder Mail Finished Mail Failed to Send at [" + GG.Global.General.GetUTCDateTime().ToString() + "].", EventLogEntryType.Error);
                            //}
                        }
                        else
                        {
                            //_eventLog.WriteEntry("No Car Insurance Renewal Reminder Mail for [" + GG.Global.General.GetUTCDateTime().ToString() + "].", EventLogEntryType.Information);
                            fileList[0] = string.Empty;
                        }
                    //}
                    //else
                    //{
                    //    //_eventLog.WriteEntry("Car Insurance Renewal Reminder Mail Started Mail Failed at [" + GG.Global.General.GetUTCDateTime().ToString() + "].", EventLogEntryType.Error);
                    //    fileList[0] = string.Empty;
                    //}
                }
                catch (Exception Ex)
                {
                    //_eventLog.WriteEntry(_step + ", Message:" + Ex.Message, EventLogEntryType.Error);
                }
                finally
                {
                    if (_sqlCmd != null)
                    {
                        _sqlCmd = null;
                    }

                    if (_sqlConn.State != ConnectionState.Closed)
                    {
                        _sqlConn.Close();
                        _sqlConn = null;
                    }
                }
           // }
        }

    }
}
